package org.darcy.controller.portal;

import org.darcy.entity.SysUser;
import org.darcy.framework.annotation.BussinessLog;
import org.darcy.framework.blog.business.enums.ArticleStatusEnum;
import org.darcy.framework.util.ResultUtil;
import org.darcy.framework.util.SessionUtil;
import org.darcy.model.BizArticleModel;
import org.darcy.service.BizArticleService;
import org.darcy.service.SysLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 网站页面
 */
@Controller
public class PortalController {
	/**
	 * sidebar部分的推荐、近期和随机tab页中显示的文章数
	 */
	private static final int SIDEBAR_ARTICLE_SIZE = 8;
	private static final String INDEX_URL = "index";

	@Autowired
	private BizArticleService bizArticleService;

	@Autowired
	private SysLinkService sysLinkService;

	/**
	 * 加载首页的数据
	 */
	private void loadIndexPage(BizArticleModel vo, Model model) {
		vo.setStatus(ArticleStatusEnum.PUBLISHED.getCode());
		IPage<BizArticleModel> pageInfo = bizArticleService.query(vo);
		model.addAttribute("page", pageInfo);
		model.addAttribute("model", vo);
		model.addAttribute("indexLinkList", sysLinkService.listOfIndex());
	}

	@RequestMapping("/")
	@BussinessLog(value = "进入首页")
	public ModelAndView index(BizArticleModel vo, Model model) {
		model.addAttribute("url", INDEX_URL);
		loadIndexPage(vo, model);
		return ResultUtil.view(INDEX_URL);
	}

	@RequestMapping("/index/{pageNumber}")
	@BussinessLog(value = "进入文章列表第{1}页")
	public ModelAndView type(@PathVariable("pageNumber") Integer pageNumber, BizArticleModel vo, Model model) {
		vo.setP(pageNumber);
		model.addAttribute("url", INDEX_URL);
		loadIndexPage(vo, model);
		return ResultUtil.view(INDEX_URL);
	}

	@GetMapping("/type/{typeId}")
	@BussinessLog(value = "进入文章分类[{1}]列表页")
	public ModelAndView type(@PathVariable("typeId") Long typeId, Model model) {
		BizArticleModel vo = new BizArticleModel();
		vo.setTypeId(typeId);
		model.addAttribute("url", "type/" + typeId);
		loadIndexPage(vo, model);

		return ResultUtil.view(INDEX_URL);
	}

	@GetMapping("/type/{typeId}/{pageNumber}")
	@BussinessLog(value = "进入文章分类[{1}]列表第{2}页")
	public ModelAndView type(@PathVariable("typeId") Long typeId, @PathVariable("pageNumber") Integer pageNumber,
			Model model) {
		BizArticleModel vo = new BizArticleModel();
		vo.setTypeId(typeId);
		vo.setP(pageNumber);
		model.addAttribute("url", "type/" + typeId);
		loadIndexPage(vo, model);
		return ResultUtil.view(INDEX_URL);
	}

	@GetMapping("/tag/{tagId}")
	@BussinessLog(value = "进入文章标签[{1}]列表页")
	public ModelAndView tag(@PathVariable("tagId") Long tagId, Model model) {
		BizArticleModel vo = new BizArticleModel();
		vo.setTagId(tagId);
		model.addAttribute("url", "tag/" + tagId);
		loadIndexPage(vo, model);

		return ResultUtil.view(INDEX_URL);
	}

	@GetMapping("/tag/{tagId}/{pageNumber}")
	@BussinessLog(value = "进入文章标签[{1}]列表第{2}页")
	public ModelAndView tag(@PathVariable("tagId") Long tagId, @PathVariable("pageNumber") Integer pageNumber,
			Model model) {
		BizArticleModel vo = new BizArticleModel();
		vo.setTagId(tagId);
		vo.setP(pageNumber);
		model.addAttribute("url", "tag/" + tagId);
		loadIndexPage(vo, model);

		return ResultUtil.view(INDEX_URL);
	}

	@GetMapping("/article/{articleId}")
	@BussinessLog(value = "进入文章[{2}]详情页")
	public ModelAndView article(Model model, @PathVariable("articleId") Long articleId) {
		BizArticleModel article = bizArticleService.getDetail(articleId);
		if (article == null || ArticleStatusEnum.UNPUBLISHED.getCode() == article.getStatus()) {
			return ResultUtil.forward("/error/404");
		}
		if (article.getRequiredAuth()) {
			SysUser sessionUser = SessionUtil.getUser();
			if (null != sessionUser) {
				article.setRequiredAuth(false);
			}
		}
		model.addAttribute("article", article);
		// 上一篇下一篇
		model.addAttribute("other", bizArticleService.getPrevAndNextArticles(article.getCreateTime()));
		// 相关文章
		model.addAttribute("relatedList", bizArticleService.listRelatedArticle(SIDEBAR_ARTICLE_SIZE, article));
		model.addAttribute("articleDetail", true);
		return ResultUtil.view("article");
	}

}
